package com.atguigu.gulimall.search.config;

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author majinhui
 * @description
 * @create 2024/7/24 23:31
 **/
@Configuration
@Slf4j
public class GulimallESConfig {

    private static ElasticsearchTransport getElasticsearchTransport() {
        // Create the low-level client
        RestClient restClient = RestClient.builder(
                new HttpHost("localhost", 9200)).build();
        // Create the transport with a Jackson mapper
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());
        return transport;
    }

    @Bean
    public ElasticsearchClient esClient() {
        ElasticsearchTransport transport = getElasticsearchTransport();
        // And create the API client
        return new ElasticsearchClient(transport);
    }

    @Bean
    public ElasticsearchAsyncClient asyncClient(){
        ElasticsearchTransport transport = getElasticsearchTransport();
        return new ElasticsearchAsyncClient(transport);

    }
}
